package cn.edu.scau.cmi.linpeiwei.controller;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Text;

import java.net.URL;
import java.util.ResourceBundle;

import com.sun.org.apache.bcel.internal.generic.NEW;

import cn.edu.scau.cmi.linpeiwei.composite.safe.SafeComponent;
import cn.edu.scau.cmi.linpeiwei.composite.safe.SafeComposite;
import cn.edu.scau.cmi.linpeiwei.composite.safe.SafeNode;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;


public class CompositeController extends AnchorPane implements Initializable {
	@FXML
	private TreeView<SafeComponent> tv;
	@FXML
	private TextField name;
	@FXML
	private Text info;
	
	private SafeComposite root;
	private SafeComposite safeComposite;
	private SafeNode safeNode;
	private TreeItem<SafeComponent> selectItem;

	@FXML
	public void onEventAdd(ActionEvent event) {
		if (selectItem != null) {
			if (selectItem.getValue() instanceof SafeComposite) {
				if (!name.getText().isEmpty()) {
					safeComposite = new SafeComposite(name.getText());
					((SafeComposite) selectItem.getValue()).add(safeComposite);
					TreeItem<SafeComponent> tmp = new TreeItem<>(safeComposite);
					selectItem.getChildren().add(tmp);
					tv.getSelectionModel().select(tmp);
				} else {
					info.setText("请在上方输入框输入节点名称");
				}

			} else {
				info.setText("非树枝节点");
			}
		} else {
			info.setText("未选择节点");
		}
	}

	@FXML
	public void onEventAddNode(ActionEvent event) {
		if (selectItem != null) {
			if (selectItem.getValue() instanceof SafeComposite) {
				if (!name.getText().isEmpty()) {
					safeNode = new SafeNode(name.getText());
					((SafeComposite) selectItem.getValue()).add(safeNode);
					TreeItem<SafeComponent> tmp = new TreeItem<>(safeNode);
					selectItem.getChildren().add(tmp);
					tv.getSelectionModel().select(tmp);
				} else {
					info.setText("请在上方输入框输入节点名称");
				}
			} else {
				info.setText("非树枝节点");
			}
		} else {
			info.setText("未选择节点");
		}

	}

	@FXML
	public void onEventDel(ActionEvent event) {
		if(selectItem != null) {
			tv.getSelectionModel().select(selectItem.getParent());
			((SafeComposite)selectItem.getParent().getValue()).delete(selectItem.getValue());
			selectItem.getParent().getChildren().remove(selectItem);
		}else {
			info.setText("未选择节点");
		}
	}

	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		// TODO 自动生成的方法存根
		root = new SafeComposite("root");
		TreeItem<SafeComponent> rootTItem = new TreeItem<>(root);
		tv.setShowRoot(true);
		tv.setRoot(rootTItem);
		tv.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
			public void handle(MouseEvent event) {
				Node node = event.getPickResult().getIntersectedNode();

				if (node instanceof Text || (node instanceof TreeCell && ((TreeCell) node).getText() != null)) {
					selectItem = (TreeItem) tv.getSelectionModel().getSelectedItem();
				}
			}
		});
	}
}
